<?php


declare (strict_types=1);

namespace app\mall\model;

use app\mall\model\MallGoods;
use app\mall\model\MallGoodsItem;
use think\admin\Model;
use think\model\relation\HasOne;

class MallOrderCart extends Model
{
    /**
     * 关联产品数据
     * @return \think\model\relation\HasOne
     */
    public function goods(): HasOne
    {
        return $this->hasOne(MallGoods::class, 'code', 'gcode');
    }

    /**
     * 关联规格数据
     * @return \think\model\relation\HasOne
     */
    public function specs(): HasOne
    {
        return $this->hasOne(MallGoodsItem::class, 'ghash', 'ghash');
    }

    public function getSpecAttr($value, $data)
    {
        $attributes = explode(";;", $data['gspec']);
        $result = [];

        foreach ($attributes as $attribute) {
            list($key, $value) = explode("::", $attribute);
            $result[$key] = $value;
        }
        return $result;
    }
}